/*
 * Copyright 2006 Marc Wick, geonames.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package ca.cbc.gisdigester.geoparser;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.jdom.Element;

import com.sun.syndication.feed.module.Module;
import com.sun.syndication.io.ModuleParser;

/**
 * SimpleParser is a parser for the GeoRSS Simple format.
 * 
 * @author Marc Wick
 * @version $Id: SimpleParser.java,v 1.3 2007/02/07 08:00:21 marcwick Exp $
 * 
 */
public class SimpleParser implements ModuleParser {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sun.syndication.io.ModuleParser#getNamespaceUri()
	 */
	public String getNamespaceUri() {
		return GeoRSSModule.GEORSS_GEORSS_URI;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sun.syndication.io.ModuleParser#parse(org.jdom.Element)
	 */
	public Module parse(Element element) {
		SimpleModuleImpl simpleModuleImpl = new SimpleModuleImpl();

		Element lat = element.getChild(GeoRSSModule.SIMPLE_ELEMENTNAME_LATITUDE, GeoRSSModule.SIMPLE_NS);
		if (lat != null) {
			simpleModuleImpl.setLatitude(Double.parseDouble(lat.getText()));
		}
		
		Element lng = element.getChild(GeoRSSModule.SIMPLE_ELEMENTNAME_LONGITUDE, GeoRSSModule.SIMPLE_NS);
		if (lng != null) {
			simpleModuleImpl.setLongitude(Double.parseDouble(lng.getText()));
		}
		
		Element simplePoint = element.getChild(GeoRSSModule.SIMPLE_ELEMENTNAME_POINT, GeoRSSModule.SIMPLE_NS);
		if (simplePoint != null) {
			String coordinates = simplePoint.getText();
			String[] coord = coordinates.split(" ");
			simpleModuleImpl.setLatitude(Double.parseDouble(coord[0]));
			simpleModuleImpl.setLongitude(Double.parseDouble(coord[1]));
		}

		Element simpleLine = element.getChild(GeoRSSModule.SIMPLE_ELEMENTNAME_LINE, GeoRSSModule.SIMPLE_NS);
		if (simpleLine != null) {
			//System.out.println("line found ....");
			String coordinates = simpleLine.getText();
			simpleModuleImpl.setLine(parseCollection(coordinates));
		}

		Element simplePolygon = element.getChild(GeoRSSModule.SIMPLE_ELEMENTNAME_POLYGON, GeoRSSModule.SIMPLE_NS);
		if (simplePolygon != null) {
			//System.out.println("polygon found ....");
			String coordinates = simplePolygon.getText();
			simpleModuleImpl.setLine(parseCollection(coordinates));
		}

		if ((lat == null || lng == null) && simplePoint == null && simpleLine == null && simplePolygon == null) {
			return (GeoRSSModule) GMLParser.parseGML(element);
		}

		return simpleModuleImpl;
	}

	private Collection parseCollection(String coordinates) {

		List list = new ArrayList();

		String[] coord = coordinates.split(" ");

		for (int i = 0; i < coord.length; i = i + 2) {
			GeoRSSPoint geoRSSPoint = new GeoRSSPoint();
			geoRSSPoint.setLatitude(Double.parseDouble(coord[i]));
			geoRSSPoint.setLongitude(Double.parseDouble(coord[i + 1]));
			list.add(geoRSSPoint);
		}
		return list;
	}

}
